package com.hongkzh.www.adv.service;

import com.hongkzh.www.conmon.RedisTool;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.params.geo.GeoRadiusParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author martins
 */
public class MediaService extends BaseService {
    private SqlPara sqlPara;

    public List<Record> getCarouselList() {
        SqlPara sqlPara = Db.getSqlPara("media.getCarouselList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public List<Record> userCategoryList(String loginId) {
        SqlPara sqlPara = Db.getSqlPara("media.userCategoryList", loginId);
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public List<Record> categoryList() {
        SqlPara sqlPara = Db.getSqlPara("media.categoryList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public List<Record> getTop10List() {
        SqlPara sqlPara = Db.getSqlPara("media.getTop10List");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public Page<Record> getHotAdvList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("media.getHotAdvList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;

    }
   private void initMediaData( ){

       //Jedis j = Redis.use().getJedis();
       if(!RedisTool.exists(StaticPublic.GEOMEDIAEXITSKEY)){
           SqlPara sqlPara = Db.getSqlPara("media.getMediaAdvList");
          List<Record> records = Db.find(sqlPara);
          if(null!=records&&records.size()>0) {
              RedisTool.setex(StaticPublic.GEOMEDIAEXITSKEY, StaticPublic.GEOMEDIAEXITSKEYTIME, "1");
              for (Record r : records) {
                  RedisTool.geoadd(StaticPublic.GEOMEDIAKEY,r.getDouble("longitude"),r.getDouble("latitude"),r.getStr("id"));
              }
          }

       }
   }
    public Page<Record> getNearbyAdvList(double latitude, double longitude, int pageNumber) {

        initMediaData();

        List<Record> records = new ArrayList<>();
        Page<Record> p = new Page<Record>(records, pageNumber, StaticPublic.PAGESIZE, 0, 0);
        List<GeoRadiusResponse> list = RedisTool.georadius(StaticPublic.GEOMEDIAKEY, longitude, latitude, 10, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withDist().sortAscending().count(6 * pageNumber));
        int totalRow=list.size();

        int totalPage = (int)(totalRow /StaticPublic.PAGESIZE);
        if (null != list && list.size() > 0) {
            list.subList(0, StaticPublic.PAGESIZE * (pageNumber - 1)).clear();
        }
        SqlPara sqlPara;
        for (GeoRadiusResponse g : list) {
            String id = new String(g.getMember());
            sqlPara = Db.getSqlPara("media.getMediaAdvById", id);
            Record r = Db.findFirst(sqlPara);
            if (null != r) {
                r.set("distance", g.getDistance());
                records.add(r);
            }
        }
        if (null != records && records.size() > 0) {
            p = new Page<Record>(records, pageNumber, StaticPublic.PAGESIZE, totalPage, totalRow);
        }
        return p;
    }
}
